<?php
/**
 * @author Feng
 * @date 2012-7-6
 */
class controller_admin_resource_keyword extends system_user_control {
	
	private $concept = null;
	public function init() {
		if(!parent::init()) {
			return ;
		}
		$this->concept = new resource_keyword_concept();
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Resource'))->setRoute('resource/index'));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Keywords'))->setRoute('resource/keyword/index'));
	}
		
	public function action_index() {
		$this->_goRoute('resource/keyword/list');
		$this->render();
	}
	
	public function action_list() {
		$checkedList = array();
		if($this->getRequest()->isPost() && $this->_post('delete')) {
			$checkedList = (array) $this->_post('id');
			try {
				system_db::getAdapter()->beginTransaction();
				$this->_auth();
				foreach($checkedList AS $id) {
					$this->concept->delete($this->concept->fetch(array('keyword.id = ?' => $id)));
				}
				system_db::getAdapter()->commit();
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				system_db::getAdapter()->rollBack();
				$this->view->throwException = $e;
			}
		}
		$this->view->checkedList = $checkedList;
		
		$adapter = new system_paginator_adapter_concept($this->concept);
		$adapter->setCurrentPageNumber(intval($this->_param('page', 1)));
		
		$this->view->order = $order = strtolower($this->_get('order'));
		$keyword = trim($this->_get('keyword'));
		if($keyword == self::$_keyword) $keyword = '';
		if($keyword) {
			$adapter->where("`name` LIKE '%$keyword%'");
		}
		$_order = array("id DESC");
		if($order) {
			$this->view->sort = $sort = intval($this->_get('sort'));
			switch($order) {
				case 'total': case 'day': case 'week': case 'month': {
					$order = 'hit_' . $order;
					break;
				}
				case 'active': {
					$order = 'is_active';
					break;
				}
				default: {
					$sort = !$sort;
					$order = 'id';
					break;
				}
			}
			$_sort = !$sort ? 'DESC' : 'ASC';
			$_order = array("$order $_sort");
		}
		$adapter->setLimit(30);
		$adapter->order($_order);
		$this->view->list = new system_paginator($adapter);
		$this->_crumbs()->addPage(system_nav_page::create($this->language('List'))->setRoute('resource/keyword/list'));
		$this->render();
	}
}
